﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    public class ShipController
    {
        public void ClearAllShippingMethods(int PortalID)
        {
            IEnumerator enumerator;
            ArrayList shippingMethodList = this.GetShippingMethodList(PortalID);
            try
            {
                enumerator = shippingMethodList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_ShippingMethodInfo current = (NB_Store_ShippingMethodInfo) enumerator.Current;
                    this.ClearAllShippingRates(PortalID, current.ShipMethodID);
                    this.DeleteShippingMethod(current.ShipMethodID);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void ClearAllShippingRates(int PortalID, int ShipMethodID)
        {
            IEnumerator enumerator;
            ArrayList shippingRateListByShipMethodID = this.GetShippingRateListByShipMethodID(PortalID, ShipMethodID);
            try
            {
                enumerator = shippingRateListByShipMethodID.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
                    this.DeleteShippingRate(Conversions.ToInteger(NewLateBinding.LateGet(objectValue, null, "ItemId", new object[0], null, null, null)));
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CopyAllShippingRates(int PortalID, int ShipMethodID, int NewShipMethodID)
        {
            IEnumerator enumerator;
            ArrayList shippingRateListByShipMethodID = this.GetShippingRateListByShipMethodID(PortalID, ShipMethodID);
            try
            {
                enumerator = shippingRateListByShipMethodID.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_ShippingRatesInfo current = (NB_Store_ShippingRatesInfo) enumerator.Current;
                    current.ShipMethodID = NewShipMethodID;
                    current.ItemId = -1;
                    this.UpdateObjShippingRate(current);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CopyShippingMethod(int ShipMethodID)
        {
            NB_Store_ShippingMethodInfo shippingMethod = this.GetShippingMethod(ShipMethodID);
            if (shippingMethod != null)
            {
                int shipMethodID = shippingMethod.ShipMethodID;
                shippingMethod.ShipMethodID = -1;
                shippingMethod.MethodName = shippingMethod.MethodName + " [Copy]";
                int newShipMethodID = this.UpdateObjShippingMethod(shippingMethod);
                this.CopyAllShippingRates(shippingMethod.PortalID, shipMethodID, newShipMethodID);
            }
        }

        public void DeleteShippingMethod(int ShipMethodID)
        {
            NB_Store_ShippingMethodInfo shippingMethod = this.GetShippingMethod(ShipMethodID);
            if (shippingMethod != null)
            {
                this.ClearAllShippingRates(shippingMethod.PortalID, ShipMethodID);
                DataProvider.Instance().DeleteShippingMethod(ShipMethodID);
            }
        }

        public void DeleteShippingRate(int ItemId)
        {
            DataProvider.Instance().DeleteNB_Store_ShippingRates(ItemId);
        }

        private decimal getCountryExtraShipping(int PortalID, string Country, decimal CartWeight, int ShipMethodID)
        {
            IEnumerator enumerator;
            IEnumerator enumerator2;
            decimal shipCost = new decimal();
            NB_Store_ShippingRatesInfo current = this.getDefaultValue(PortalID, "DCO", ShipMethodID);
            if (current == null)
            {
                shipCost = new decimal();
            }
            else if (Versioned.IsNumeric(current.ShipCost))
            {
                shipCost = current.ShipCost;
            }
            ArrayList list = this.GetShippingRateList(PortalID, "COU", ShipMethodID);
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    current = (NB_Store_ShippingRatesInfo) enumerator.Current;
                    if ((!current.Disable && current.Description.StartsWith("XX - ")) && ((((decimal.Compare(current.Range1, CartWeight) <= 0) & (decimal.Compare(current.Range2, CartWeight) >= 0)) | ((decimal.Compare(current.Range1, decimal.Zero) == 0) & (decimal.Compare(current.Range2, decimal.Zero) == 0))) && Versioned.IsNumeric(current.ShipCost)))
                    {
                        shipCost = current.ShipCost;
                        goto Label_014B;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        Label_014B:
            list = this.GetShippingRateList(PortalID, "COU", ShipMethodID);
            try
            {
                enumerator2 = list.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    current = (NB_Store_ShippingRatesInfo) enumerator2.Current;
                    if ((!current.Disable && current.Description.StartsWith(Country + " - ")) && ((((decimal.Compare(current.Range1, CartWeight) <= 0) & (decimal.Compare(current.Range2, CartWeight) >= 0)) | ((decimal.Compare(current.Range1, decimal.Zero) == 0) & (decimal.Compare(current.Range2, decimal.Zero) == 0))) && Versioned.IsNumeric(current.ShipCost)))
                    {
                        return current.ShipCost;
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            return shipCost;
        }

        private NB_Store_ShippingRatesInfo getDefaultValue(int PortalID, string ShipType, int ShipMethodID)
        {
            ArrayList list = this.GetShippingRateList(PortalID, ShipType, ShipMethodID);
            if (list == null)
            {
                return null;
            }
            if (list.Count == 0)
            {
                return null;
            }
            return (NB_Store_ShippingRatesInfo) list[0];
        }

        private bool getFreeShipping(int PortalID, decimal CartTotal, int ShipMethodID)
        {
            NB_Store_ShippingRatesInfo info = this.getDefaultValue(PortalID, "FRE", ShipMethodID);
            if (((info != null) && Versioned.IsNumeric(info.ShipCost)) && ((decimal.Compare(CartTotal, info.ShipCost) > 0) & (decimal.Compare(info.ShipCost, decimal.Zero) >= 0)))
            {
                string str = SharedFunctions.GetStoreSetting(PortalID, "shipfree.list", SharedFunctions.GetCurrentCulture());
                if (str == "")
                {
                    return true;
                }
                if (Strings.InStr(str.ToLower(), CurrentCart.GetCurrentCart(PortalID).CountryCode.ToLower(), CompareMethod.Binary) > 0)
                {
                    return true;
                }
            }
            return false;
        }

        private decimal getPriceShipping(int PortalID, decimal Prc, int ShipMethodID)
        {
            IEnumerator enumerator;
            decimal minusOne = decimal.MinusOne;
            ArrayList list = this.GetShippingRateList(PortalID, "PRC", ShipMethodID);
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_ShippingRatesInfo current = (NB_Store_ShippingRatesInfo) enumerator.Current;
                    if ((!current.Disable && ((decimal.Compare(current.Range1, Prc) <= 0) & (decimal.Compare(current.Range2, Prc) >= 0))) && Versioned.IsNumeric(current.ShipCost))
                    {
                        return current.ShipCost;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return minusOne;
        }

        private decimal getProductExtraShipping(int PortalID, ArrayList orderDetailInfoList, int ShipMethodID)
        {
            NB_Store_CartItemsInfo current;
            NB_Store_ShippingRatesInfo info2;
            IEnumerator enumerator;
            IEnumerator enumerator5;
            decimal num2 = new decimal();
            Hashtable hashtable = new Hashtable();
            bool flag = false;
            try
            {
                enumerator = orderDetailInfoList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    current = (NB_Store_CartItemsInfo) enumerator.Current;
                    info2 = this.GetShippingRateByObjID(PortalID, current.ModelID, "PRD", ShipMethodID);
                    if (info2 != null)
                    {
                        hashtable.Add(current.ItemID, info2);
                        if (decimal.Compare(info2.Range1, decimal.Zero) > 0)
                        {
                            flag = true;
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            if (flag)
            {
                IEnumerator enumerator2;
                Hashtable hashtable2 = new Hashtable();
                try
                {
                    enumerator2 = orderDetailInfoList.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        current = (NB_Store_CartItemsInfo) enumerator2.Current;
                        info2 = (NB_Store_ShippingRatesInfo) hashtable[current.ItemID];
                        if ((info2 != null) && !hashtable2.ContainsKey("Box" + info2.ShipCost.ToString()))
                        {
                            hashtable2.Add("Box" + info2.ShipCost.ToString(), info2.ShipCost);
                        }
                    }
                }
                finally
                {
                    if (enumerator2 is IDisposable)
                    {
                        (enumerator2 as IDisposable).Dispose();
                    }
                }
                decimal num3 = new decimal();
                IDictionaryEnumerator enumerator3 = hashtable2.GetEnumerator();
                while (enumerator3.MoveNext())
                {
                    IEnumerator enumerator4;
                    DictionaryEntry entry = (DictionaryEntry) enumerator3.Current;
                    decimal num4 = new decimal();
                    try
                    {
                        enumerator4 = orderDetailInfoList.GetEnumerator();
                        while (enumerator4.MoveNext())
                        {
                            current = (NB_Store_CartItemsInfo) enumerator4.Current;
                            info2 = (NB_Store_ShippingRatesInfo) hashtable[current.ItemID];
                            if ((info2 != null) && Operators.ConditionalCompareObjectEqual(entry.Key, "Box" + info2.ShipCost.ToString(), false))
                            {
                                if ((decimal.Compare(info2.Range1, 100M) > 0) | (decimal.Compare(info2.Range1, decimal.Zero) <= 0))
                                {
                                    info2.Range1 = 100M;
                                }
                                num4 = decimal.Add(num4, decimal.Multiply(new decimal(current.Quantity), info2.Range1));
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator4 is IDisposable)
                        {
                            (enumerator4 as IDisposable).Dispose();
                        }
                    }
                    if (decimal.Compare(num4, decimal.Zero) > 0)
                    {
                        num3 = decimal.Add(num3, decimal.Multiply(Math.Ceiling(decimal.Divide(num4, 100M)), Conversions.ToDecimal(entry.Value)));
                    }
                }
                return num3;
            }
            try
            {
                enumerator5 = orderDetailInfoList.GetEnumerator();
                while (enumerator5.MoveNext())
                {
                    current = (NB_Store_CartItemsInfo) enumerator5.Current;
                    info2 = (NB_Store_ShippingRatesInfo) hashtable[current.ItemID];
                    if ((info2 != null) && Versioned.IsNumeric(info2.ShipCost))
                    {
                        num2 = decimal.Add(num2, decimal.Multiply(info2.ShipCost, new decimal(current.Quantity)));
                    }
                }
            }
            finally
            {
                if (enumerator5 is IDisposable)
                {
                    (enumerator5 as IDisposable).Dispose();
                }
            }
            return num2;
        }

        private decimal getQuantityShipping(int PortalID, ArrayList orderDetailInfoList, int ShipMethodID)
        {
            int num2;
            IEnumerator enumerator;
            IEnumerator enumerator2;
            decimal minusOne = decimal.MinusOne;
            try
            {
                enumerator = orderDetailInfoList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                    num2 += current.Quantity;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            ArrayList list = this.GetShippingRateList(PortalID, "QTY", ShipMethodID);
            try
            {
                enumerator2 = list.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    NB_Store_ShippingRatesInfo info2 = (NB_Store_ShippingRatesInfo) enumerator2.Current;
                    if ((!info2.Disable && ((decimal.Compare(info2.Range1, new decimal(num2)) <= 0) & (decimal.Compare(info2.Range2, new decimal(num2)) >= 0))) && Versioned.IsNumeric(info2.ShipCost))
                    {
                        return info2.ShipCost;
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            return minusOne;
        }

        public decimal getShippingCost(int PortalID, string CountryCode, int ShipMethodID)
        {
            decimal num7;
            decimal num8;
            decimal minusOne = new decimal();
            OrderController controller = new OrderController();
            decimal cartItemTotal = CurrentCart.GetCartItemTotal(PortalID);
            if (this.getFreeShipping(PortalID, cartItemTotal, ShipMethodID))
            {
                return decimal.Zero;
            }
            ArrayList currentCartItems = CurrentCart.GetCurrentCartItems(PortalID);
            this.getWeightShipping(PortalID, currentCartItems, ref num8, ref num7, ShipMethodID);
            decimal num5 = this.getQuantityShipping(PortalID, currentCartItems, ShipMethodID);
            decimal num4 = this.getPriceShipping(PortalID, cartItemTotal, ShipMethodID);
            if (((decimal.Compare(num4, decimal.Zero) > 0) & ((decimal.Compare(num4, num5) <= 0) | (decimal.Compare(num5, decimal.Zero) < 0))) & ((decimal.Compare(num4, num8) <= 0) | (decimal.Compare(num8, decimal.Zero) <= 0)))
            {
                minusOne = num4;
            }
            if (((decimal.Compare(num5, decimal.Zero) > 0) & ((decimal.Compare(num5, num8) <= 0) | (decimal.Compare(num8, decimal.Zero) <= 0))) & ((decimal.Compare(num5, num4) <= 0) | (decimal.Compare(num4, decimal.Zero) < 0)))
            {
                minusOne = num5;
            }
            if (decimal.Compare(num8, minusOne) > 0)
            {
                minusOne = num8;
            }
            decimal num3 = this.getProductExtraShipping(PortalID, currentCartItems, -1);
            minusOne = decimal.Add(minusOne, num3);
            num3 = this.getCountryExtraShipping(PortalID, CountryCode, num7, ShipMethodID);
            if ((decimal.Compare(minusOne, decimal.Zero) > 0) | (decimal.Compare(num3, decimal.Zero) == 0))
            {
                minusOne = decimal.Add(minusOne, num3);
            }
            else
            {
                minusOne = num3;
            }
            if (SharedFunctions.GetDefaultShipMethod(PortalID) == ShipMethodID)
            {
                if (decimal.Compare(minusOne, decimal.Zero) < 0)
                {
                    minusOne = decimal.MinusOne;
                }
            }
            else if (decimal.Compare(minusOne, decimal.Zero) <= 0)
            {
                minusOne = decimal.MinusOne;
            }
            return minusOne;
        }

        public NB_Store_ShippingMethodInfo GetShippingMethod(int ShipMethodID)
        {
            return (NB_Store_ShippingMethodInfo) CBO.FillObject(DataProvider.Instance().GetShippingMethod(ShipMethodID), typeof(NB_Store_ShippingMethodInfo));
        }

        public ArrayList GetShippingMethodEnabledList(int PortalID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetShippingMethodEnabledList(PortalID), typeof(NB_Store_ShippingMethodInfo));
        }

        public ArrayList GetShippingMethodList(int PortalID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetShippingMethodList(PortalID), typeof(NB_Store_ShippingMethodInfo));
        }

        public NB_Store_ShippingRatesInfo GetShippingRate(int PortalID, int ItemId)
        {
            return (NB_Store_ShippingRatesInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_ShippingRates(PortalID, ItemId), typeof(NB_Store_ShippingRatesInfo));
        }

        public NB_Store_ShippingRatesInfo GetShippingRateByObjID(int PortalID, int ObjectId, string ShipType, int ShipMethodID)
        {
            return (NB_Store_ShippingRatesInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_ShippingRatesByObjID(PortalID, ObjectId, ShipType, ShipMethodID), typeof(NB_Store_ShippingRatesInfo));
        }

        public ArrayList GetShippingRateList(int PortalID, string ShipType, int ShipMethodID)
        {
            return this.GetShippingRateList(PortalID, ShipType, "", "", -1, ShipMethodID);
        }

        public ArrayList GetShippingRateList(int PortalID, string ShipType, string Lang, string Filter, int ShipMethodID)
        {
            return this.GetShippingRateList(PortalID, ShipType, Lang, Filter, -1, ShipMethodID);
        }

        public ArrayList GetShippingRateList(int PortalID, string ShipType, string Lang, string Filter, int CategoryID, int ShipMethodID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_ShippingRatess(PortalID, ShipType, Lang, Filter, CategoryID, ShipMethodID), typeof(NB_Store_ShippingRatesInfo));
        }

        public ArrayList GetShippingRateListByShipMethodID(int PortalID, int ShipMethodID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_ShippingRatesListByShipMethodID(PortalID, ShipMethodID), typeof(NB_Store_ShippingRatesInfo));
        }

        private void getWeightShipping(int PortalID, ArrayList orderDetailInfoList, ref decimal weightCost, ref decimal CartWeight, int ShipMethodID)
        {
            decimal num2;
            decimal productWeight;
            IEnumerator enumerator;
            IEnumerator enumerator2;
            decimal shipCost = new decimal();
            NB_Store_ShippingRatesInfo info2 = this.getDefaultValue(PortalID, "DPO", -1);
            if (info2 == null)
            {
                productWeight = new decimal();
            }
            else if (Versioned.IsNumeric(info2.ProductWeight))
            {
                productWeight = info2.ProductWeight;
            }
            try
            {
                enumerator = orderDetailInfoList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartItemsInfo current = (NB_Store_CartItemsInfo) enumerator.Current;
                    info2 = this.GetShippingRateByObjID(PortalID, current.ModelID, "PRD", -1);
                    if (info2 == null)
                    {
                        num2 = decimal.Add(num2, decimal.Multiply(productWeight, new decimal(current.Quantity)));
                    }
                    else if (Versioned.IsNumeric(info2.ProductWeight))
                    {
                        num2 = decimal.Add(num2, decimal.Multiply(info2.ProductWeight, new decimal(current.Quantity)));
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            ArrayList list = this.GetShippingRateList(PortalID, "WEI", ShipMethodID);
            try
            {
                enumerator2 = list.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    info2 = (NB_Store_ShippingRatesInfo) enumerator2.Current;
                    if ((!info2.Disable && ((decimal.Compare(info2.Range1, num2) <= 0) & (decimal.Compare(info2.Range2, num2) >= 0))) && Versioned.IsNumeric(info2.ShipCost))
                    {
                        shipCost = info2.ShipCost;
                        goto Label_01CB;
                    }
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
        Label_01CB:
            weightCost = shipCost;
            CartWeight = num2;
        }

        public int UpdateObjShippingMethod(NB_Store_ShippingMethodInfo objInfo)
        {
            return DataProvider.Instance().UpdateShippingMethod(objInfo.ShipMethodID, objInfo.PortalID, objInfo.MethodName, objInfo.MethodDesc, objInfo.SortOrder, objInfo.TemplateName, objInfo.Disabled, objInfo.URLtracker);
        }

        public void UpdateObjShippingRate(NB_Store_ShippingRatesInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_ShippingRates(objInfo.PortalID, objInfo.ItemId, objInfo.Range1, objInfo.Range2, objInfo.ObjectId, objInfo.ShipCost, objInfo.ShipType, objInfo.Disable, objInfo.Description, objInfo.ProductWeight, objInfo.ProductHeight, objInfo.ProductLength, objInfo.ProductWidth, objInfo.ShipMethodID);
        }
    }
}

